<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search_div">
                <div class="add">
                    <Button v-if="role < 3 " size="default" icon="md-add" type="primary" @click="add" style="margin-right:10px">新增工位/办公室</Button>
                    <div v-if="role < 3 " class="importExcel">
                        <Button size="default" icon="md-cloud-upload" type="warning">批量导入</Button>
                        <input class="imBtn" type="file" @change="upFile($event)" accept=".cvs,.xls,.xlsx">

                    </div>
                    <Button v-if="role < 3 " size="default" icon="md-download" type="success" @click="downTmp" style="margin-left:10px"> 下载模板 </Button>
                    <Button v-if="role < 3 " size="default" icon="md-download" type="success" @click="down_all_qrcode(-1)" style="margin-left:10px"> 批量下载二维码 </Button>
                    <Button v-if="role < 3 " size="default" type="info" @click="create_qrcode" style="margin-left:10px"> 生成二维码 </Button>
                </div>

                <Form :model="search" class="search_box" inline label-position="right">

                    <FormItem label="分部 -> 楼层 -> 区域：">
                        <Cascader class="cascader" :value="cascadeArr" :data="cascadeList" change-on-select @on-change="cascadeSelection" :clearable="false"></Cascader>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                    </FormItem>
                </Form>
            </div> <!-- end search -->

            <Table ref="table" :loading="loading" stripe :columns="columns1" :data="data1"></Table>
            <Page :total="total" :current="search.page" show-sizer show-total :page-size="search.rows" @on-page-size-change="getListByPageSize" @on-change="getListByPageNum"> </Page>

            <Spin v-show="is_show_my_loading" fix class="demo-spin-col">
                <Icon type="ios-loading" size=25 class="demo-spin-icon-load"></Icon>
                <div v-html="loading_msg"> </div>
            </Spin>
        </Card>

        <Modal v-model="is_show_qrcode" title="二维码" :mask-closable="false">
            <div class="code_div">
                <img :src="qrcode_url" alt="">
                <p style="font-size:16px;font-weight:700; padding-top:10px" class="channel-link"> {{ws_name}} </p>
            </div>
            <div slot="footer">
                <Button type="default" size="large" @click="is_show_qrcode = false">取消</Button>
                <Button v-if="role < 3 " type="primary" size="large" @click="down_all_qrcode(wsid)">下载</Button>
            </div>
        </Modal>

    </div>
</template>
<script>
    import Cookies from 'js-cookie'
    import {
        mapMutations,
        mapGetters
    } from 'vuex'
    import {
        exportExcel
    } from '@/api/data'
    import axios from '@/libs/api.request'
    import config from '@/config'

    export default {
        name: 'workstation-team',
        data() {
            return {
                // 加载信息
                loading_msg: '',
                is_show_my_loading: false,
                // 工位id
                wsid: '',
                // 级联回显数组
                cascadeArr: [-1],

                // 二维码地址
                qrcode_url: '',
                ws_name: '',

                // 是否显示二维码
                is_show_qrcode: false,

                // 加载状态
                loading: false,

                // 搜索对象
                search: {
                    branchid: -1,
                    floorid: -1,
                    regionid: -1,
                    wsid: -1,
                    page: 1,
                    rows: 10,
                },

                // 数据总条数
                total: 0,

                // 模块列表
                columns1: [{
                        type: 'index',
                        align: 'center',
                        width: 60
                    },
                    {
                        title: '编号',
                        align: 'center',
                        key: 'number'
                    },
                    {
                        title: '类型',
                        align: 'center',
                        key: 'work_type_zw'
                    },
                    {
                        title: '价格',
                        align: 'center',
                        key: 'price'
                    },
                    {
                        title: '扫描次数',
                        align: 'center',
                        render: (h, params) => {
                            return h('div', {
                                style: {
                                    color: '#2A69C6',
                                    cursor: 'pointer'
                                },
                                on: {
                                    click: () => {
                                        this.myClose('scan-detail')
                                        let data = {
                                            number: params.row.number,
                                            regionid: params.row.regionid,
                                            branchid: params.row.branchid,
                                            floorid: params.row.floorid,
                                        }
                                        this.$_changePage('scan-detail', '扫描明细', data)
                                    }
                                }
                            }, params.row.wss_num)
                        }
                    },
                    {
                        width: 300,
                        renderHeader: (h) => {
                            return h('div', {
                                style: {
                                    width: 280 + 'px',
                                    textAlign: 'center'
                                }
                            }, '公司名称')
                        },
                        key: 'company_name'
                    },
                    {
                        title: '当前区域',
                        align: 'center',
                        key: 'region_zw'
                    },
                    {
                        title: '当前楼层',
                        align: 'center',
                        key: 'floor_zw'
                    },
                    {
                        title: '当前分部',
                        align: 'center',
                        key: 'branch_zw'
                    },
                    {
                        title: '到期时间',
                        align: 'center',
                        key: 'company_out_time'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 380,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'success',
                                        size: 'default',
                                    },
                                    style: {
                                        marginRight: '14px',
                                        height: '30px'
                                    },
                                    on: {
                                        click: () => {
                                            this.is_show_qrcode = true
                                            this.qrcode_url = params.row.wechat_mini_code_pic
                                            this.wsid = params.row.id
                                            this.ws_name = `${params.row.branch_zw}-${params.row.floor_zw}-${params.row.number}`
                                        }
                                    }
                                }, '二维码'),
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'default',
                                        icon: 'ios-information-circle-outline'
                                    },
                                    style: {
                                        marginRight: '14px',
                                        height: '30px'
                                    },
                                    on: {
                                        click: () => {
                                            this.myClose('workstation-details')
                                            this.$_changePage('workstation-details', '工位/办公室详情', {
                                                wsid: params.row.id
                                            })
                                        }
                                    }
                                }, '工位/办公室详情'),
                                h('Poptip', {
                                        props: {
                                            confirm: true,
                                            title: '你确定要删除吗?',
                                            placement: 'top',
                                            transfer: true
                                        },
                                        on: {

                                            'on-ok': () => {
                                                if (this.role > 2) {
                                                    return false
                                                }
                                                this.deleteWorkStationByid(params.row.id)
                                            }
                                        }
                                    },
                                    [h(
                                        'Button', {
                                            props: {
                                                type: 'error',
                                                size: 'default',
                                                icon: 'ios-trash',
                                                disabled: this.role > 2
                                            },
                                            style: {
                                                marginRight: '14px',
                                                width: '80px',
                                                height: '30px'
                                            }
                                        },
                                        '删除'
                                    )]
                                )
                            ])
                        }
                    }

                ],
                // 区域数据
                data1: []
            }
        },
        methods: {
            ...mapMutations(['myClose']),
            ...mapGetters(['getBranchList', 'getRole']),

            // 级联选择
            cascadeSelection(value) {
                if (value.length == 1) {
                    this.search.branchid = value[0]
                    this.search.floorid = -1
                    this.search.regionid = -1
                    return false
                }
                if (value.length == 2) {
                    this.search.branchid = value[0]
                    this.search.floorid = value[1]
                    this.search.regionid = -1
                    return false
                }
                this.search.branchid = value[0]
                this.search.floorid = value[1]
                this.search.regionid = value[2]
            },

            // 获取工位列表
            getWorkStation() {
                this.loading = true
                let {
                    ...data
                } = this.search
                axios.ajax_post('admin-work_station-ajax_get_ws', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.total = res.data.data.total_num

                        if (this.data1.length < 1 && this.total > 0) {
                            this.search.page = Math.ceil(this.total / this.search.rows)
                            this.getWorkStation()
                        }
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })
            },

            // 新增
            add() {
                this.$_changePage('workstation-add', '新增工位/办公室', {})
            },

            // 搜索
            searchList() {
                this.search.page = 1
                this.search.rows = 10
                this.getWorkStation()
            },

            // 删除工位
            deleteWorkStationByid(id) {
                let data = {
                    wsid: id
                }
                axios.ajax_post('admin-work_station-ajax_del_ws', data).then(res => {
                    if (res.data.code === 1) {
                        this.$Notice.success({
                            title: '删除成功',
                            duration: 3
                        })
                        this.getWorkStation()
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })
            },

            // 批量导入
            upFile(e) {
                let file = e.target.files[0]
                let data = new FormData()
                data.append('ws_file', file)
                data.append('token', Cookies.get('token'))

                axios.uploadFileRequest('admin-work_station-import_ws', data).then(res => {
                    if (res.data.code === 1) {
                        this.$Notice.success({
                            title: res.data.msg,
                            duration: 3
                        })
                        this.getWorkStation()
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })
            },

            // 下载模板
            downTmp() {
                location.href = config.conf.download_prefix + 'template/workstation_template.xlsx'
            },

            // 生成二维码
            create_qrcode() {
                this.is_show_my_loading = true
                this.loading_msg = '二维码生成中.... <br /> 此过程耗时较长，请耐心等待'
                axios.ajax_post('admin-work_station-ajax_gen_ws_mini_code', {}).then(res => {
                    this.is_show_my_loading = false
                    if (res.data.code === 1) {
                        this.$Notice.success({
                            title: res.data.msg,
                            duration: 3
                        })
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })
            },

            // 批量下载二维码
            down_all_qrcode(wsid) {
                this.$Modal.confirm({
                    title: '下载提示',
                    content: '此过程耗时较长，尽量选择小于100条进行下载',

                    okText: '下载',
                    cancelText: '重新选择',
                    onOk: () => {
                        this.is_show_my_loading = true
                        this.loading_msg = '二维码下载中.... <br /> 此过程耗时较长，请耐心等待'
                        let {
                            ...out
                        } = this.search

                        if (wsid) {
                            out.wsid = wsid
                        }
                        out.page = 0
                        out.rows = 0

                        out.token = Cookies.get('token')
                        let itemUrl = `${config.conf.download_prefix + 'admin-work_station-ajax_down_ws_mini_code'}?`

                        // 编辑请求数据
                        for (const key in out) {
                            itemUrl += `${key}=${out[key]}&`
                        }
                        itemUrl = itemUrl.slice(0, -1)
                        let net = window.open(itemUrl)

                        net.addEventListener("beforeunload", (e) => {
                            this.is_show_my_loading = false
                        })
                    },
                    onCancel: () => {

                    }
                })
            },
            getListByPageSize(rows) {
                this.search.rows = rows
                this.getWorkStation()
            },

            getListByPageNum(page) {
                this.search.page = page
                this.getWorkStation()
            },
        },
        computed: {
            cascadeList() {
                let list = JSON.parse(JSON.stringify(this.getBranchList()))
                list.unshift({
                    value: -1,
                    label: '全部'
                })
                return list
            },
            role() {
                return this.getRole()
            }
        },

        created() {
            if (Object.keys(this.$route.params).length === 3) {
                this.search.branchid = this.$route.params.branchid
                this.search.floorid = this.$route.params.floorid
                this.search.regionid = this.$route.params.regionid
                this.cascadeArr = [this.$route.params.branchid, this.$route.params.floorid, this.$route.params.regionid]
            }
            this.getWorkStation()
        },
        activated() {
            if (Object.keys(this.$route.params).length === 3) {
                if (this.cascadeArr[0] != this.$route.params.branchid || this.cascadeArr[1] != this.$route.params.floorid || this.cascadeArr[2] != this.$route.params.regionid) {
                    this.search.branchid = this.$route.params.branchid
                    this.search.floorid = this.$route.params.floorid
                    this.search.regionid = this.$route.params.regionid
                    this.cascadeArr = [this.$route.params.branchid, this.$route.params.floorid, this.$route.params.regionid]
                    this.getWorkStation()
                }
            }
        }
    }
</script>
<style lang="less" scoped>
    .add {
        margin-right: 20px;
    }

    .importExcel {
        position: relative;
        display: inline-block;
        margin-left: 10px;

        .imBtn {
            position: absolute;
            left: 0;
            opacity: 0;
            width: 100px;
            height: 32px;
            z-index: 999;
        }
    }

    .search_div {
        .search_box {
            width: auto
        }
    }

    .modalFooter {
        display: flex;
        justify-content: flex-end;
    }

    .cascader {
        width: 325px;
    }

    .branchSelect {
        width: 15vw;

    }

    .code_div {
        text-align: center;

        img {
            width: 200px;
        }
    }

    .demo-spin-icon-load {
        animation: ani-demo-spin 1s linear infinite;
    }

    @keyframes ani-demo-spin {
        from {
            transform: rotate(0deg);
        }

        50% {
            transform: rotate(180deg);
        }

        to {
            transform: rotate(360deg);
        }
    }
</style>